# -*- coding:utf8 -*-
# !/usr/bin/python
'''
author: ning.luo
createTime: 2021/8/06 18:40
update: ning.luo
updateTime: 2021/8/19 10:05
description: 2楼值班提醒定时任务
'''
import json
import requests
import logging
from datetime import datetime

error_logger = logging.getLogger("error")
info_logger = logging.getLogger("info")


class WeChatMessage(object):
    """
    微信机器人发送消息
    文档：
    https://open.work.weixin.qq.com/api/doc/90000/90136/91770
    """

    def sent_msg(self, receiver_path_list, msg, at_mobiles:list):
        if not at_mobiles:
            at_mobiles = []
        try:
            for receiver_path in receiver_path_list:
                headers = {'Content-Type': 'application/json;charset=utf-8'}
                data = {
                    "msgtype": "news",
                    "news": {
                        "articles": [
                            {
                                "title": "值班提醒",
                                "description": msg,
                                "url": "www.baidu.com",
                                "picurl": "https://img2.baidu.com/it/u=2102736929,2417598652&fm=26&fmt=auto&gp=0.jpg",
                                "mentioned_list": [],
                                "mentioned_mobile_list": at_mobiles
                            }
                        ]
                    }
                }
                requests.post(receiver_path, data=json.dumps(data), headers=headers)
        except Exception as e:
            error_logger.error("微信消息发送失败： %s" % e)


if __name__ == '__main__':
    phone_list = ["18664642558","13810974467","13632680067","15927017493","13760104011",
                  "18667172816","15986818010"]
    a = WeChatMessage()
    d = datetime.today()
    if d.isoweekday() < 7:
        phone = phone_list[d.isoweekday()-1]
        if isinstance(phone, str):
            phone = [phone]
        print(phone)
        a.sent_msg(
            receiver_path_list=[
                "https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=c183a02d-4b18-4dea-9c70-7cccac97f22f"],
            msg= "今晚当班哦",
            at_mobiles=phone
        )